package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Client;
import net.ib.mntalk.protocol.Client.DeliverReadMarksReq;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.ReadMark;
import net.ib.mtalk.database.helper.ChatDatabaseHelper;
import net.ib.mtalk.network.NetResultReceiver.Receiver;
import net.ib.mtalk.network.TcpClient;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

/**
 * COMMENT : subscribe할 때 DeliverReadMark API에 setting되는 callback class<br>
 *    AsyncNetTask 클래스를 상속받지 않는 Task<br>
 *    DeliverReadMark 요청 후 받는 Client API이므로 <br> 
 *    NetResultReceiver에 사용될 Receiver다<br>
 *    서버에서 DeliverReadMark API가 호출되면 callback이 onNetResult()함수로 들어온다.
 *    onNetResult()는 서버에서 잘 받았다는 ACK를 보내고 받은 메시지를 Database에 저장하고 끝난다.
 *    
 */
public class DeliverReadMarksTask implements Receiver, NotifyingAsyncQueryHandler.AsyncQueryListener {
	
	private Context mContext;
	private NotifyingAsyncQueryHandler mDbHandler;
	private Handler mUiHandler;
	
	public DeliverReadMarksTask(Context _context, Handler handler) {
		mContext = _context;
		mDbHandler = new NotifyingAsyncQueryHandler(mContext.getContentResolver(), this);
		mUiHandler = handler;
	}
	
	@Override
	public void onNetResult(int resultCode, Object request, Object result) {
		// 받은 메시지를 처리해야 한다. DeliverMessagesRes를 보내줘야 한다.

		TcpEnvelope received = (TcpEnvelope) result;

		Client.DeliverReadMarksReq deliverReadMarksReq = (Client.DeliverReadMarksReq)received.getMessage();

		Client.DeliverReadMarksRes deliverReadMarksRes = new Client.DeliverReadMarksRes();
		deliverReadMarksRes.setResponseCode(Common.SUCCESS);

		TcpEnvelope response = new TcpEnvelope.Builder()
			.setRequestId(received.getRequestId())
			.setType(TcpEnvelope.RESPONSE)
			.setMethodId(received.getMethodId())
			.setMessage(deliverReadMarksRes)
			.build();

		// Response!
		TcpClient.send(response);
		
		doAfterGetResult(deliverReadMarksReq);
	}

	@Override
	public void onNetError(int resultCode, Object sentObject) {
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
	}

	@Override
	public void onDeleteComplete(int token, Object cookie, int result) {
	}

	@Override
	public void onInsertComplete(int token, Object cookie, Uri uri) {
	}

	@Override
	public void onUpdateComplete(int token, Object cookie, int result) {
	}
	
	protected void doAfterGetResult(DeliverReadMarksReq deliverReadMarksReq) {
		MTalkLogWriter.d("DeliverReadMarksTask.doAfterGetResult()");
		if(!deliverReadMarksReq.hasReadMark()){
			return;
		}
		ReadMark readMark = deliverReadMarksReq.getReadMark();
		if(!readMark.hasNotiUuid() && !readMark.hasMsgUuid() && !readMark.hasUserUuid()){
			return;
		}
		ChatDatabaseHelper.updateUnreadCount(mContext, readMark);
		// TODO : ReadMark 작업
		Log.i("ReadMark", "readMark.getMsgUuid() : " + GpbSupport.UuidMsgToString(readMark.getMsgUuid()));
		Log.i("ReadMark", "readMark.getNotiUuid() : " + GpbSupport.UuidMsgToString(readMark.getNotiUuid()));
		Log.i("ReadMark", "readMark.getUserUuid() : " + GpbSupport.UuidMsgToString(readMark.getUserUuid()));
	}
}
